<?php

class AdminController extends Controller {

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    // 新增管理员帐号
    public function actionCreate() {
        $model = new Admin;

        $this->performAjaxValidation($model);

        if (isset($_POST['Admin'])) {
            unset($_POST['Admin']['confirmPassword']);
            $model->attributes = $_POST['Admin'];
            if ($model->save())
                $this->redirect(array('index'));
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    // 修改管理员密码
    public function actionUpdate() {
        $model = $this->loadModel(user()->id);

        $this->performAjaxValidation($model);

        if (isset($_POST['Admin'])) {
            $model->attributes = $_POST['Admin'];
            if ($model->save())
                $this->redirect(array('site/index'));
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    // 删除管理员帐号
    public function actionDelete($id) {
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }

    /**
     * Lists all models.
     */
//    public function actionIndex() {
//        $dataProvider = new CActiveDataProvider('Admin');
//        $this->render('index', array(
//            'dataProvider' => $dataProvider,
//        ));
//    }
    public function actionIndex() {
        $model = new Admin('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['Admin']))
            $model->attributes = $_GET['Admin'];

        $this->render('index', array(
            'model' => $model,
        ));
    }

    /**
     * Manages all models.
     */
    public function actionAdmin() {
        $model = new Admin('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['Admin']))
            $model->attributes = $_GET['Admin'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id) {
        $model = Admin::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'admin-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
